package com.lefevre.cms.web.action.follow;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lefevre.cms.bean.*;
import com.lefevre.cms.bean.follow.Follow;
import com.lefevre.cms.bean.follow.Follower;
import com.lefevre.cms.bean.user.User;
import com.lefevre.cms.service.follow.FollowService;
import com.lefevre.cms.service.setting.SettingService;
import com.lefevre.cms.service.user.UserService;
import com.lefevre.cms.utils.JsonUtils;
import com.lefevre.cms.web.action.fileSystem.FileManage;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 关注
 *
 * @author Lefevre
 */
@Controller
public class FollowController {

    @Resource
    FollowService followService;
    @Resource
    SettingService settingService;
    @Resource
    UserService userService;
    @Resource
    FileManage fileManage;

    /**
     * 关注列表
     *
     * @param id       用户Id
     * @param userName 用户名称
     */
    @ResponseBody
    @RequestMapping("/control/follow/list")
    public String followList(PageForm pageForm, @RequestParam Long id, @RequestParam String userName, HttpServletRequest request) throws Exception {
        Map<String, String> error = new HashMap<>();
        Map<String, Object> returnValue = new HashMap<>();
        if (id != null && id > 0L && userName != null && !"".equals(userName.trim())) {
            PageView<Follow> pageView = new PageView<>(settingService.findSystemSetting_cache().getBackstagePageNumber(), pageForm.getPage(), 10, request.getRequestURI(), request.getQueryString());
            int firstIndex = (pageForm.getPage() - 1) * pageView.getMaxresult();

            Page<Follow> followPage = followService.page(new Page<>(firstIndex, pageView.getMaxresult()), null);
            QueryResult<Follow> qr = new QueryResult<>();

            if (!ObjectUtils.isEmpty(followPage.getRecords())) {
                for (Follow follow : followPage.getRecords()) {
                    User user = userService.findUserByUserName(follow.getFriendUserName());
                    if (user != null) {
                        follow.setFriendNickname(user.getNickname());
                        if (!StrUtil.isBlank(user.getAvatarName())) {
                            follow.setFriendAvatarPath(fileManage.fileServerAddress() + user.getAvatarPath());
                            follow.setFriendAvatarName(user.getAvatarName());
                        }
                    }
                }
            }
            qr.setResultlist(followPage.getRecords());
            qr.setTotalrecord(followPage.getTotal());

            pageView.setQueryResult(qr);

            User user = userService.findUserById(id);
            if (user != null) {
                returnValue.put("currentUser", user);
            }
            returnValue.put("pageView", pageView);
        } else {
            error.put("userId", "用户Id或用户名称不能为空");
        }
        if (error.size() > 0) {
            return JsonUtils.toJSONString(new RequestResult(ResultCode.FAILURE, error));
        } else {
            return JsonUtils.toJSONString(new RequestResult(ResultCode.SUCCESS, returnValue));
        }
    }


    /**
     * 粉丝列表
     *
     * @param id       用户Id
     * @param userName 用户名称
     */
    @ResponseBody
    @RequestMapping("/control/follower/list")
    public String followerList(PageForm pageForm, @RequestParam Long id, @RequestParam String userName, HttpServletRequest request) throws Exception {
        Map<String, String> error = new HashMap<>();
        Map<String, Object> returnValue = new HashMap<>();
        if (id != null && id > 0L && userName != null && !"".equals(userName.trim())) {
            PageView<Follower> pageView = new PageView<>(settingService.findSystemSetting_cache().getBackstagePageNumber(), pageForm.getPage(), 10, request.getRequestURI(), request.getQueryString());
            int firstIndex = (pageForm.getPage() - 1) * pageView.getMaxresult();

            QueryResult<Follower> qr = followService.findFollowerByUserName(id, userName, firstIndex, pageView.getMaxresult());
            if (qr != null && qr.getResultlist() != null && qr.getResultlist().size() > 0) {
                for (Follower follower : qr.getResultlist()) {
                    User user = userService.findUserByUserName(follower.getFriendUserName());
                    if (user != null) {
                        follower.setFriendNickname(user.getNickname());
                        if (user.getAvatarName() != null && !"".equals(user.getAvatarName().trim())) {
                            follower.setFriendAvatarPath(fileManage.fileServerAddress() + user.getAvatarPath());
                            follower.setFriendAvatarName(user.getAvatarName());
                        }
                    }
                }
            }
            //将查询结果集传给分页List
            pageView.setQueryResult(qr);
            User user = userService.findUserById(id);
            if (user != null) {
                returnValue.put("currentUser", user);
            }
            returnValue.put("pageView", pageView);
        } else {
            error.put("userId", "用户Id或用户名称不能为空");
        }
        if (error.size() > 0) {
            return JsonUtils.toJSONString(new RequestResult(ResultCode.FAILURE, error));
        } else {
            return JsonUtils.toJSONString(new RequestResult(ResultCode.SUCCESS, returnValue));
        }
    }
}
